package com.chenbei.controller.admin.erp;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenbei.common.domain.vo.ApiResponse;
import com.chenbei.common.domain.vo.Pagination;
import com.chenbei.common.domain.vo.TreeNode;
import com.chenbei.common.domain.vo.UploadFileResult;
import com.chenbei.common.util.ApiResponseUtil;
import com.chenbei.erp.domain.Product;
import com.chenbei.erp.domain.ProductBrand;
import com.chenbei.erp.domain.ProductCategory;
import com.chenbei.erp.domain.ProductUnit;
import com.chenbei.erp.service.IProductBrandService;
import com.chenbei.erp.vo.req.ProductBrandReq;
import com.chenbei.erp.vo.req.ProductCategoryReq;
import com.chenbei.erp.vo.req.ProductReq;
import com.chenbei.erp.vo.req.ProductUnitReq;
import com.chenbei.erp.service.IProductCategoryService;
import com.chenbei.erp.service.IProductService;
import com.chenbei.erp.service.IProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "产品管理")
@RestController("ERPProduct")
@RequestMapping("/admin/erp/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductCategoryService productCategoryService;

    @Autowired
    private IProductUnitService productUnitService;

    @Autowired
    private IProductBrandService productBrandService;

    @Operation(summary = "新增产品")
    @PostMapping("add")
    public ApiResponse<Object> add(@Valid @RequestBody Product product) {
        productService.add(product);
        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新产品")
    @PostMapping("update")
    public ApiResponse<Object> update(@Valid @RequestBody Product product) {
        productService.update(product);
        return ApiResponseUtil.success();
    }

    @Operation(summary = "删除产品")
    @PostMapping("deleteById/{id}")
    public ApiResponse<Object> deleteById(@PathVariable Long id) {
        productService.delete(id);
        return ApiResponseUtil.success();
    }

    @Operation(summary = "产品分页列表")
    @GetMapping("pageList")
    public ApiResponse<Pagination<Product>> pageList(ProductReq productQuery) {
        Page<Product> productPage = productService.pageList(productQuery);
        return ApiResponseUtil.success(productPage);
    }

    @Operation(summary = "产品列表")
    @GetMapping("list")
    public ApiResponse<List<Product>> list(ProductReq productQuery) {
        List<Product> list = productService.list(productQuery);
        return ApiResponseUtil.success(list);
    }

    @Operation(summary = "新增产品分类")
    @PostMapping("categoryAdd")
    public ApiResponse<Object> categoryAdd(@Valid @RequestBody ProductCategory ProductCategory) {
        productCategoryService.add(ProductCategory);
        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新产品分类")
    @PostMapping("categoryUpdate")
    public ApiResponse<Object> categoryUpdate(@Valid @RequestBody ProductCategory ProductCategory) {
        productCategoryService.update(ProductCategory);
        return ApiResponseUtil.success();
    }

    @Operation(summary = "获取产品分类树结构")
    @GetMapping("categoryTree")
    public ApiResponse<List<TreeNode<ProductCategory>>> categoryTree(ProductCategoryReq ProductCategoryReq) {
        List<TreeNode<ProductCategory>> tree = productCategoryService.tree(ProductCategoryReq);
        return ApiResponseUtil.success(tree);
    }

    @Operation(summary = "获取产品分类列表")
    @GetMapping("categoryList")
    public ApiResponse<List<ProductCategory>> categoryList(ProductCategoryReq ProductCategoryReq) {
        List<ProductCategory> list = productCategoryService.list(ProductCategoryReq);
        return ApiResponseUtil.success(list);
    }

    @Operation(summary = "删除产品分类")
    @PostMapping("categoryDeleteById/{id}")
    public ApiResponse<Object> categoryDeleteById(@PathVariable Long id) {
        productCategoryService.delete(id);
        return ApiResponseUtil.success();
    }

    @Operation(summary = "新增产品单位")
    @PostMapping("unitAdd")
    public ApiResponse<Object> unitAdd(@Valid @RequestBody ProductUnit ProductUnit) {
        productUnitService.add(ProductUnit);
        return ApiResponseUtil.success();
    }

    @Operation(summary = "更新产品单位")
    @PostMapping("unitUpdate")
    public ApiResponse<Object> unitUpdate(@Valid @RequestBody ProductUnit ProductUnit) {
        productUnitService.update(ProductUnit);
        return ApiResponseUtil.success();
    }

    @Operation(summary = "获取产品单位分页列表")
    @GetMapping("unitPageList")
    public ApiResponse<Pagination<ProductUnit>> unitPageList(ProductUnitReq ProductUnitReq) {
        Page<ProductUnit> productUnitPage = productUnitService.pageList(ProductUnitReq);
        return ApiResponseUtil.success(productUnitPage);
    }

    @Operation(summary = "删除产品单位")
    @PostMapping("unitDeleteById/{id}")
    public ApiResponse<Object> unitDeleteById(@PathVariable Long id) {
        productUnitService.delete(id);
        return ApiResponseUtil.success();
    }

    @Operation(summary = "获取产品单位下拉列表")
    @GetMapping("unit/selectList")
    public ApiResponse<List<ProductUnit>> selectList() {
        List<ProductUnit> list = productUnitService.list(new ProductUnitReq());
        return ApiResponseUtil.success(list);
    }

    @Operation(summary = "新增产品品牌")
    @PostMapping("brandAdd")
    public ApiResponse<Boolean> brandAdd(@RequestBody ProductBrand productBrand) {
        productBrandService.add(productBrand);
        return ApiResponseUtil.success(true);
    }

    @Operation(summary = "产品品牌图片上传")
    @PostMapping("brandUploadFile")
    public ApiResponse<UploadFileResult> brandUploadFile(MultipartFile file) {
        UploadFileResult uploadFileResult = productBrandService.uploadFile(file);
        return ApiResponseUtil.success(uploadFileResult);
    }

    @Operation(summary = "更新产品品牌")
    @PostMapping("brandUpdate")
    public ApiResponse<Boolean> brandUpdate(@RequestBody ProductBrand productBrand) {
        productBrandService.update(productBrand);
        return ApiResponseUtil.success(true);
    }

    @Operation(summary = "品牌分页列表")
    @GetMapping("brandPageList")
    public ApiResponse<Pagination<ProductBrand>> brandPageList(ProductBrandReq productBrandReq) {
        Page<ProductBrand> productBrandPage = productBrandService.pageList(productBrandReq);
        return ApiResponseUtil.success(productBrandPage);
    }

    @Operation(summary = "品牌列表")
    @GetMapping("brandList")
    public ApiResponse<List<ProductBrand>> brandList(ProductBrandReq productBrandReq) {
        List<ProductBrand> list = productBrandService.list(productBrandReq);
        return ApiResponseUtil.success(list);
    }
}
